using UnityEngine;

public class RateLayerContoller : MonoBehaviour
{
	private class RateDialogFlowScreen
	{
		private string message;

		private bool canDifferAction;

		private OnDialogComplete onComplete;

		public string yesButtonLabel = "Yes";

		public string noButtonLabel = "No";

		public RateDialogFlowScreen(string message, bool canDifferAction, string yesButtonLabel, string noButtonLabel, OnDialogComplete onComplete)
		{
			this.message = message;
			this.canDifferAction = canDifferAction;
			this.onComplete = onComplete;
			this.yesButtonLabel = yesButtonLabel;
			this.noButtonLabel = noButtonLabel;
		}

		public string GetMessage()
		{
			return message;
		}

		public bool ShouldShowAskLaterButton()
		{
			return canDifferAction;
		}

		public void DoOnComplete(bool complete)
		{
			if (onComplete != null)
			{
				onComplete(complete);
			}
		}
	}

	private class RateDialogFlowBuilder
	{
		private RateDialogFlowScreen currentScreen;

		private RateDialogFlowScreen firstScreen;

		private RateDialogFlowScreen suggestionScreen;

		private RateDialogFlowScreen rateScreen;

		public bool shouldContinue { get; private set; }

		public RateDialogFlowBuilder()
		{
			//firstScreen = GetNewEnjoyingGameDialog();
			//suggestionScreen = GetNewSuggestionDialog();
			//rateScreen = GetNewRateDialog();
		}

		private RateDialogFlowScreen GetNewEnjoyingGameDialog()
		{
			return new RateDialogFlowScreen("Are you enjoying Table Tennis Champion?", false, "Yes", "No", OnAskWhetherEnjoying);
		}

		private RateDialogFlowScreen GetNewRateDialog()
		{
			return new RateDialogFlowScreen("Support this game by rating it on the store?", true, "Support Us!", "Later", OnRateDialog);
		}

		private RateDialogFlowScreen GetNewSuggestionDialog()
		{
			return new RateDialogFlowScreen("Sorry to hear that:( Do you want to comment how we can improve it?", true, "Improve it!", "No", OnSuggestionDialog);
		}

		public void OnAskWhetherEnjoying(bool likedGame)
		{
			if (likedGame)
			{
				currentScreen = rateScreen;
			}
			else
			{
				currentScreen = suggestionScreen;
			}
			shouldContinue = true;
			ScriptableObjectSingleton<RateSettings>.instance.OnUserEnjoying(likedGame);
		}

		public void OnRateDialog(bool wantsToRate)
		{
			if (wantsToRate)
			{
				GGSupportMenu.instance.showRateApp(ConfigBase.instance.rateProvider);
				GGPlayerSettings.instance.Model.playerRatedApp = true;
				GGPlayerSettings.instance.Save();
				ScriptableObjectSingleton<RateSettings>.instance.OnUserRated();
			}
			shouldContinue = false;
		}

		public void OnSuggestionDialog(bool hasSuggestion)
		{
			if (hasSuggestion)
			{
				GGSupportMenu.instance.SubmitFeedback(GGPlayerSettings.instance.Model.name, GGServerConstants.instance.appName, BehaviourSingletonInit<GGServerRequestsBackend>.instance.cachedPlayerId);
				GGPlayerSettings.instance.Model.playerRatedApp = true;
				GGPlayerSettings.instance.Save();
				ScriptableObjectSingleton<RateSettings>.instance.OnUserRated();
			}
			shouldContinue = false;
		}

		public RateDialogFlowScreen GetActiveScreen()
		{
			return currentScreen;
		}

		public void ResetScreensToStart()
		{
			currentScreen = firstScreen;
		}
	}

	public delegate void OnDialogComplete(bool complete);

	public UILabel textLabel;

	public UILabel yesLabel;

	public UILabel noLabel;

	public GameObject askLaterButton;

	private RateDialogFlowBuilder rateBuilder = new RateDialogFlowBuilder();

	private void OnEnable()
	{
		ScriptableObjectSingleton<RateSettings>.instance.OnRateDialogShown();
		rateBuilder.ResetScreensToStart();
		DisplayDialog();
	}

	private void DisplayDialog()
	{
		RateDialogFlowScreen activeScreen = rateBuilder.GetActiveScreen();
		UITools.ChangeText(textLabel, activeScreen.GetMessage());
		UITools.ChangeText(yesLabel, activeScreen.yesButtonLabel);
		UITools.ChangeText(noLabel, activeScreen.noButtonLabel);
	}

	public void OnYes()
	{
		RateDialogFlowScreen activeScreen = rateBuilder.GetActiveScreen();
		activeScreen.DoOnComplete(true);
		if (rateBuilder.shouldContinue)
		{
			DisplayDialog();
		}
		else
		{
			NavigationManager.instance.Pop(true);
		}
	}

	public void OnNo()
	{
		RateDialogFlowScreen activeScreen = rateBuilder.GetActiveScreen();
		activeScreen.DoOnComplete(false);
		if (rateBuilder.shouldContinue)
		{
			DisplayDialog();
		}
		else
		{
			NavigationManager.instance.Pop(true);
		}
	}

	public void OnLater()
	{
		NavigationManager.instance.Pop(true);
	}
}
